<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="包号" prop="packageNumber">
        <el-input v-model="formData.packageNumber" placeholder="请输入包号" />
      </el-form-item>
      <el-form-item label="第一方" prop="firstParty">
        <el-input v-model="formData.firstParty" placeholder="请输入第一方" />
      </el-form-item>
      <el-form-item label="第二方" prop="secondParty">
        <el-input v-model="formData.secondParty" placeholder="请输入第二方" />
      </el-form-item>
      <el-form-item label="接口标识类型" prop="interfaceType">
        <el-select v-model="formData.interfaceType" placeholder="请选择接口标识类型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="接口流水号" prop="interfaceSerialNumber">
        <el-input v-model="formData.interfaceSerialNumber" placeholder="请输入接口流水号" />
      </el-form-item>
      <el-form-item label="发布方" prop="publisher">
        <el-input v-model="formData.publisher" placeholder="请输入发布方" />
      </el-form-item>
      <el-form-item label="接收方" prop="receiver">
        <el-input v-model="formData.receiver" placeholder="请输入接收方" />
      </el-form-item>
      <el-form-item label="关键接口" prop="isKeyInterface">
        <el-radio-group v-model="formData.isKeyInterface">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="设计院" prop="designInstitute">
        <el-input v-model="formData.designInstitute" placeholder="请输入设计院" />
      </el-form-item>
      <el-form-item label="接口详细描述" prop="interfaceDescription">
        <Editor v-model="formData.interfaceDescription" height="150px" />
      </el-form-item>
      <el-form-item label="CNCS转发" prop="cncsForwarding">
        <el-input v-model="formData.cncsForwarding" placeholder="请输入CNCS转发" />
      </el-form-item>
      <el-form-item label="初版计划提交时间" prop="initialPlannedSubmissionDate">
        <el-date-picker
          v-model="formData.initialPlannedSubmissionDate"
          type="date"
          value-format="x"
          placeholder="选择初版计划提交时间"
        />
      </el-form-item>
      <el-form-item label="终版计划提交时间" prop="finalPlannedSubmissionDate">
        <el-date-picker
          v-model="formData.finalPlannedSubmissionDate"
          type="date"
          value-format="x"
          placeholder="选择终版计划提交时间"
        />
      </el-form-item>
      <el-form-item label="终版计划关闭时间" prop="finalPlannedClosureDate">
        <el-date-picker
          v-model="formData.finalPlannedClosureDate"
          type="date"
          value-format="x"
          placeholder="选择终版计划关闭时间"
        />
      </el-form-item>
      <el-form-item label="CNCS负责人" prop="cncsPersonInCharge">
        <el-input v-model="formData.cncsPersonInCharge" placeholder="请输入CNCS负责人" />
      </el-form-item>
      <el-form-item label="IITF/IICS" prop="iitfIics">
        <el-input v-model="formData.iitfIics" placeholder="请输入IITF/IICS" />
      </el-form-item>
      <el-form-item label="版本" prop="version">
        <el-input v-model="formData.version" placeholder="请输入版本" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="信息交换日期" prop="informationExchangeDate">
        <el-date-picker
          v-model="formData.informationExchangeDate"
          type="date"
          value-format="x"
          placeholder="选择信息交换日期"
        />
      </el-form-item>
      <el-form-item label="信息交换传递单号" prop="informationExchangeDeliveryNumber">
        <el-input v-model="formData.informationExchangeDeliveryNumber" placeholder="请输入信息交换传递单号" />
      </el-form-item>
      <el-form-item label="参考信函" prop="referenceLetter">
        <el-input v-model="formData.referenceLetter" placeholder="请输入参考信函" />
      </el-form-item>
      <el-form-item label="发文方" prop="sendingParty">
        <el-input v-model="formData.sendingParty" placeholder="请输入发文方" />
      </el-form-item>
      <el-form-item label="收文方" prop="receivingParty">
        <el-input v-model="formData.receivingParty" placeholder="请输入收文方" />
      </el-form-item>
      <el-form-item label="所答复传递单" prop="responseDeliveryNote">
        <el-input v-model="formData.responseDeliveryNote" placeholder="请输入所答复传递单" />
      </el-form-item>
      <el-form-item label="审查意见" prop="reviewComments">
        <el-input v-model="formData.reviewComments" placeholder="请输入审查意见" />
      </el-form-item>
      <el-form-item label="附件名称" prop="attachmentName">
        <el-input v-model="formData.attachmentName" placeholder="请输入附件名称" />
      </el-form-item>
      <el-form-item label="内部要求答复日期" prop="internalResponseDueDate">
        <el-date-picker
          v-model="formData.internalResponseDueDate"
          type="date"
          value-format="x"
          placeholder="选择内部要求答复日期"
        />
      </el-form-item>
      <el-form-item label="预警值" prop="warningValue">
        <el-input v-model="formData.warningValue" placeholder="请输入预警值" />
      </el-form-item>
      <el-form-item label="答复负责人" prop="responsePersonInCharge">
        <el-input v-model="formData.responsePersonInCharge" placeholder="请输入答复负责人" />
      </el-form-item>
      <el-form-item label="是否关闭" prop="isClosed">
        <el-radio-group v-model="formData.isClosed">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { DesignIcmApi, DesignIcmVO } from '@/api/datastore/designicm'

/** 接口管理 表单 */
defineOptions({ name: 'DesignIcmForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  packageNumber: undefined,
  firstParty: undefined,
  secondParty: undefined,
  interfaceType: undefined,
  interfaceSerialNumber: undefined,
  publisher: undefined,
  receiver: undefined,
  isKeyInterface: undefined,
  designInstitute: undefined,
  interfaceDescription: undefined,
  cncsForwarding: undefined,
  initialPlannedSubmissionDate: undefined,
  finalPlannedSubmissionDate: undefined,
  finalPlannedClosureDate: undefined,
  cncsPersonInCharge: undefined,
  iitfIics: undefined,
  version: undefined,
  status: undefined,
  informationExchangeDate: undefined,
  informationExchangeDeliveryNumber: undefined,
  referenceLetter: undefined,
  sendingParty: undefined,
  receivingParty: undefined,
  responseDeliveryNote: undefined,
  reviewComments: undefined,
  attachmentName: undefined,
  internalResponseDueDate: undefined,
  warningValue: undefined,
  responsePersonInCharge: undefined,
  isClosed: undefined,
})
const formRules = reactive({
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await DesignIcmApi.getDesignIcm(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as DesignIcmVO
    if (formType.value === 'create') {
      await DesignIcmApi.createDesignIcm(data)
      message.success(t('common.createSuccess'))
    } else {
      await DesignIcmApi.updateDesignIcm(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    packageNumber: undefined,
    firstParty: undefined,
    secondParty: undefined,
    interfaceType: undefined,
    interfaceSerialNumber: undefined,
    publisher: undefined,
    receiver: undefined,
    isKeyInterface: undefined,
    designInstitute: undefined,
    interfaceDescription: undefined,
    cncsForwarding: undefined,
    initialPlannedSubmissionDate: undefined,
    finalPlannedSubmissionDate: undefined,
    finalPlannedClosureDate: undefined,
    cncsPersonInCharge: undefined,
    iitfIics: undefined,
    version: undefined,
    status: undefined,
    informationExchangeDate: undefined,
    informationExchangeDeliveryNumber: undefined,
    referenceLetter: undefined,
    sendingParty: undefined,
    receivingParty: undefined,
    responseDeliveryNote: undefined,
    reviewComments: undefined,
    attachmentName: undefined,
    internalResponseDueDate: undefined,
    warningValue: undefined,
    responsePersonInCharge: undefined,
    isClosed: undefined,
  }
  formRef.value?.resetFields()
}
</script>